package com.ljkj.cordial.chat.presenter;

import com.lanjiu.lib.NettyHost;
import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.ims.send.IMSMessageSender;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.lengthtcpheartbeat.IpAddressUtil;
import com.ljkj.cordial.lengthtcpheartbeat.TcpLengthHeartbeatBroadcastReceive;

public class NettyRequestTempManage {
    private static class BUILD {
        public static NettyRequestTempManage INSTANCE = new NettyRequestTempManage();
    }

    public static NettyRequestTempManage getInstance() {
        return BUILD.INSTANCE;
    }

    private IMSMessageSender sender;

    public NettyRequestTempManage() {
        sender = new IMSMessageSender();
        String[] address = NettyHost.BUSINESS_COMMUNICATION_HOST.split(":");
        TcpLengthHeartbeatBroadcastReceive.connect(IpAddressUtil.getIMEI(ChatApp.getInstance()), address[0], Integer.parseInt(address[1]));
//        TcpLengthHeartbeatBroadcastReceive.updateUserId();
    }

    /**
     * 删除好友
     *
     * @param friendUserId
     * @param isRegister
     */
    public void deleteFriend(String friendUserId, boolean isRegister, OnEventShortResponseListener listener) {
        MessageEvent m = new MessageEvent(isRegister ? EventConstants.Action.FRIEND_DELETE_REGISTERED : EventConstants.Action.FRIEND_DELETE_TOURISTS);
        m.setData(friendUserId);
        TcpLengthHeartbeatBroadcastReceive.send(sender.getSend(m).getUnifiedEntranceMessage(), listener);
    }

    /**
     * 游客登录
     *
     * @param listener
     */
    public void registerTour(OnEventShortResponseListener listener) {
        TcpLengthHeartbeatBroadcastReceive.send(sender.getSend(MessageEvent.createSenderEvent(EventConstants.Action.LOGIN_TOURISTS)).getUnifiedEntranceMessage(), listener);
    }

    /**
     * 注册用户登录
     *
     * @param listener
     */
    public void registerLogin(String userName, String password, OnEventShortResponseListener listener) {
        RegisteredUserDto registeredUser = new RegisteredUserDto();
        registeredUser.setUserId(userName);
        registeredUser.setSecret(password);
        MessageEvent event = MessageEvent.createSenderEvent(EventConstants.Action.LOGIN_REGISTERED);
        event.setData(registeredUser.toJson());
        TcpLengthHeartbeatBroadcastReceive.send(sender.getSend(event).getUnifiedEntranceMessage(), listener);
    }
}
